package com.aio.news;

import java.util.List;

import org.apache.http.NameValuePair;

import android.animation.Animator;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aio.news.net.HttpInterface;
import com.aio.news.net.HttpUtils;
import com.aio.news.net.HttpUtils.Method;
import com.aio.news.net.HttpUtils.RequestState;
import com.meilishuo.gson.Gson;

public abstract class BaseFragment extends Fragment {

	public abstract View initView(LayoutInflater inflater);

	public abstract List<NameValuePair> getParams(RequestState state);

	public abstract void RequestSuccess(String response, RequestState state);

	public abstract void RequestFail(String errString, RequestState state);

	public abstract void afterCreated();

	protected int LIMIT = 20;

	protected Gson mGson = new Gson();

	protected ProgressDialog pDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = initView(inflater);
		root.setOnClickListener(OnClickListener);
		AioApplication.getInjector().injectView(root, BaseFragment.this);
		afterCreated();
		return root;
	}

	View.OnClickListener OnClickListener = new View.OnClickListener() {// 防止点击事件穿透

		@Override
		public void onClick(View v) {
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	public void showDialog(String content) {
		pDialog = ProgressDialog.show(getActivity(), "", content, true, true);
	}

	public void showDialog(int stringId) {
		if (getActivity() != null)
			pDialog = ProgressDialog.show(getActivity(), "",
					getString(stringId), true, true);
	}

	public void dismissDialog() {
		if (pDialog != null && pDialog.isShowing()) {
			pDialog.dismiss();
		}
	}

	protected void getMessage(final RequestState state, HttpUtils.Method method) {
		getMessage(state, method, null, null,false);
	}
	
	protected void getMessage(final RequestState state, HttpUtils.Method method,boolean refresh) {
		getMessage(state, method, null, null, refresh);
	}

	protected void getMessage(final RequestState state,
			HttpUtils.Method method, final String fileKey, final byte[] b,
			boolean refresh) {
		if (!state.isRequestServer) {
			if(refresh || !state.isBottom){
				if (refresh) {
					state.isRefresh = refresh;
					state.isBottom = false;
					state.page = 0;
					state.tid = "";
				}
				state.isRequestServer = true;
				if (method == Method.GET) {
					HttpUtils.doAsyncGet(getParams(state), state.requestUrl,
							new HttpInterface() {
								@Override
								public void success(String content) {
									RequestSuccess(content, state);
								}

								@Override
								public void failed(String content) {
									RequestFail(content, state);
								}
							});
				} else {
					HttpUtils.doAsyncPost(getParams(state), state.requestUrl,
							fileKey, b, new HttpInterface() {
								@Override
								public void success(String content) {
									RequestSuccess(content, state);
								}

								@Override
								public void failed(String content) {
									RequestFail(content, state);
								}
							});
				}
			}
		}
	}

	public void startFullFragment(Class<? extends Fragment> clas,
			Bundle argument) {
		startFullFragment(clas, argument, R.id.second_content);
	}

	public void startFullFragment(Class<? extends Fragment> clas,
			Bundle argument, int viewId) {
		Fragment mFragment = Fragment
				.instantiate(getActivity(), clas.getName());
		if (argument != null) {
			mFragment.setArguments(argument);
		}
		FragmentTransaction mTransaction = getFragmentManager()
				.beginTransaction();
		mTransaction.setCustomAnimations(R.animator.fragment_inte_animation,
				R.animator.fragment_out_animation,
				R.animator.fragment_inte_animation,
				R.animator.fragment_out_animation);
		mTransaction.add(viewId, mFragment);
		mTransaction.addToBackStack(clas.getSimpleName());
		mTransaction.commit();
	}

	public void back() {
		FragmentManager manager = getFragmentManager();
		if (manager.getBackStackEntryCount() > 0) {
			manager.popBackStack();
		}
	}

	@Override
	public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
		return super.onCreateAnimator(transit, enter, nextAnim);
	}

}
